﻿using System;
using System.Collections;
using System.Web;
using System.Web.Caching;

namespace Nasa8x.Mvc.Helpers
{
    public static class CacheWrapper
    {
        private static readonly Cache _cache = HttpContext.Current.Cache;

        public static object Get(string _key)
        {
            return _cache.Get(_key);
        }



        public static object Remove(string _key)
        {
            return HttpContext.Current.Cache.Remove(_key);

        }


        public static void Add(string _key, object _value)
        {
            _cache.Insert(_key, _value, null, DateTime.MaxValue, TimeSpan.Zero);
        }

        public static void Add(string _key, object _value, double _seconds)
        {
            if (_seconds > 0)
                _cache.Insert(_key, _value, null, DateTime.Now.AddSeconds(_seconds), TimeSpan.Zero);
        }


        public static void Add(string _key, object _value, string _fileDependency, double minutes)
        {
            _cache.Insert(_key, _value, new CacheDependency(_fileDependency), Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(minutes));
        }

        public static void Add(string _key, object _value, string[] _fileDependencies, double _minutes)
        {
            _cache.Insert(_key, _value, new CacheDependency(_fileDependencies), Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(_minutes));
        }

        public static void Add(string _key, object _value, CacheDependency _dep)
        {

            _cache.Insert(_key, _value, _dep, DateTime.MaxValue, TimeSpan.Zero);

        }

        public static void Clear()
        {

            IDictionaryEnumerator _ie = _cache.GetEnumerator();
            while (_ie.MoveNext())
            {
                _cache.Remove(_ie.Key.ToString());
            }

        }
    }
}
